package org.authsum.stitches.wicket.content;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.validation.validator.StringValidator;
import org.authsum.stitches.external.*;
import org.authsum.stitches.wicket.BasePanel;
import org.authsum.stitches.wicket.DetailPanel;
import org.authsum.stitches.wicket.SearchParms;
import org.authsum.stitches.wicket.contentdef.ContentDefSelectionListener;
import  org.authsum.stitches.wicket.contentdef.detail.*;
import  org.authsum.stitches.wicket.*;
import  org.authsum.stitches.wicket.content.ContentSelectionListener;
import  org.authsum.stitches.wicket.content.table.*;
import  org.authsum.stitches.wicket.content.detail.*;




public class ContentsPanel  extends BaseLandingPanel implements ContentSelectionListener{
	
	private static Log logger = LogFactory.getLog(ContentsPanel.class);
	//private String keywords = new String();
	
	
	public ContentsPanel(String id) {
		super(id);
		this.contentSelectionListener = this;
		ContentHolder contentHolder = new ContentHolder();

		detailPanel = new ContentPanel("detailPanel", contentHolder,this);// ,PackageAttractionModal);
		postInit();
		
	}
	
	public ContentsPanel(String id,ContentSelectionListener contentSelectionListener) {
		super(id);
		this.contentSelectionListener = contentSelectionListener;

		ContentHolder contentHolder = new ContentHolder();

		detailPanel = new ContentPanel("detailPanel", contentHolder,this);// ,PackageAttractionModal);
		postInit();
		
	}
	private ContentSelectionListener contentSelectionListener = null;

	private void refreshTable(ContentResponse contentResponse) {
		initTable(contentResponse.getContentHolders().getContentHolder());

		backToSearchResults.setVisible(false);
		detailPanel.setVisible(false);
		centerDefaultPanel.setVisible(false);
	}
	

	
	
	protected void  refreshTable(AjaxRequestTarget target, ContentResponse contentResponse) {
		
		refreshTable(contentResponse);
		//DMIResult dmiResult = getDMIResult(dmiRequest);
		//backToSearchResults.setVisible(false);
		//detailPanel.setVisible(false);
		
		
		//initTable(dmiResult);

		
		target.addComponent(updateContainer);
		//target.addComponent(detailPanel);
		target.addComponent(backToSearchResults);
		target.addComponent(feedbackPanel);
		//target.addComponent(this);
	

	}
	

	public void editContent(AjaxRequestTarget target, ContentHolder cdf) {
		// TODO Auto-generated method stub
		// ImageContent imageContent = image.getImageContent();
		// image has been selected
		System.out.println("editContent 2");

		try {
			// need to get the freshing imagecontent, the one that was passed in
			// is from search results
			ContentRequest crequest = new ContentRequest();
			crequest.setContentId(cdf.getId());
			ContentResponse cresponse = getStitchesService().findContentById(crequest);
			editContent(cresponse.getContentHolders()
					.getContentHolder().get(0));
			//backToSearchResults.setVisible(true);
			target.addComponent(updateContainer);
		} catch (Exception e) {
			e.printStackTrace();
			error("An error occurred while trying to edit this event.");
			error(e.getMessage());
			//target.addComponent(feedbackPanel);
		}
		
	}



	

	
	

	@Override
	public void createDetailObject() {
		ContentHolder cdh = new ContentHolder();


		editContent(cdh);
		
	}

	public void contentSelected(AjaxRequestTarget target, ContentHolder contentHolder) {
		System.out.println("contentSelected");
		editContent(target,contentHolder);
		//editContentDef(target, contentDefHolder);
	}
	private void editContent(ContentHolder contentHolder) {
		System.out.println("editContentDef2");
		detailPanel = new ContentPanel("detailPanel", contentHolder,this);
		editDetailObject();
	}
	
	
	@Override
	protected void initTable(List docs) {
		//String id,List docs,ContentSelectionListener contentSelectionListener
		
		resultsTable = new ContentTable("resultsTable", docs,contentSelectionListener);
		updateContainer.addOrReplace(resultsTable);
	}

	@Override
	public void performSearch(AjaxRequestTarget target, Form form) {
		SearchParms searchParms = (SearchParms) searchForm.getModel()
		.getObject();
		ContentRequest contentRequest = new ContentRequest();
		contentRequest.setKeywords(searchParms.getKeywords());
		
		ContentResponse contentResponse = getStitchesService().findContents(contentRequest);
		
		//DMIResult dmiResult= getDMIResult(dmiRequest);
		 refreshTable(target, contentResponse);
		 searchTips.setVisible(contentResponse.getContentHolders().getContentHolder().isEmpty());

		
	}

}
